<?php declare(strict_types=1);

use function Hyperf\Support\{make};
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
//use HyperfExt\Auth\Contracts\AuthManagerInterface;


function user_func(): string
{
    return 'hello';
}

//if (!function_exists('auth')) {
//    /**
//     * Auth认证辅助方法
//     * @param string|null $guard
//     * @return \HyperfExt\Auth\Contracts\GuardInterface|\HyperfExt\Auth\Contracts\StatefulGuardInterface|\HyperfExt\Auth\Contracts\StatelessGuardInterface
//     */
//    function auth(string $guard = null)
//    {
//        if (is_null($guard)) $guard = config('auth.default.guard');
//        return make(AuthManagerInterface::class)->guard($guard);
//    }
//}

if (!function_exists('request')) {
    /**
     * 请求方法辅助方法
     * @return RequestInterface|mixed
     */
    function request()
    {
        return make(RequestInterface::class);
    }
}

if (!function_exists('response')) {
    /**
     * 响应方法辅助方法
     * @return ResponseInterface|mixed
     */
    function response()
    {
        return make(ResponseInterface::class);
    }
}

if(!function_exists('password_hash_pbkdf2')){
    function password_hash_pbkdf2($password){
        $iterations=886;
        $length= 32;
        $salt = openssl_random_pseudo_bytes(8);
        $salt_encode=base64_encode($salt);
        $hash = hash_pbkdf2("sha256", $password, $salt, $iterations, $length);
        return $hash.$salt_encode;
    }
}

if(!function_exists('password_verify_pbkdf2')){
    function password_verify_pbkdf2($password,$hash){
        $iterations = 886;
        $length = 32;
        $passhash=substr($hash,0,$length);
        $salt=base64_decode(substr($hash,$length));
        $passhash2=hash_pbkdf2("sha256", $password, $salt, $iterations, $length);
        if($passhash==$passhash2){
            return true;
        }
        return false;
    }
}
/**
 * 适配lin-cms成功输出结果
 * @param int $httpCode
 * @param string $message
 * @param int $code
 * @return mixed
 */
function success($httpCode=201,$message='success',$code=0){
    $output = [
        'code' => $code,
        'message' => $message,
        'request' => request()->getRequestUri()
    ];
    return make(ResponseInterface::class)->withHeader('content-type','application/json')
        ->withStatus($httpCode)
        ->withBody(new SwooleStream(json_encode($output)));
}

/**
 * 根据公司规范定义输出信息
 * @param $httpCode
 * @param $data
 * @param string $message
 * @param int $code
 * @return mixed
 */
function show($httpCode, $data, $message = 'ok', $code = 0)
{
    $output = [
        'code' => $code,
        'data' => $data,
        'message' => $message
    ];
    return make(ResponseInterface::class)->withHeader('content-type','application/json')
        ->withStatus($httpCode)
        ->withBody(new SwooleStream(json_encode($output)));

}

function split_modules($auths, $key = 'module')
{
    if (empty($auths)) {
        return [];
    }

    $items = [];
    $result = [];

    foreach ($auths as $key => $value) {
        if (isset($items[$value['module']])) {
            $items[$value['module']][] = $value;
        } else {
            $items[$value['module']] = [$value];
        }
    }
    foreach ($items as $key => $value) {
        $item = [
            $key => $value
        ];
        array_push($result, $item);
    }
    return $result;

}
